﻿using System.Web.SessionState;
using System.Web.UI;
using com.nm.Cache;
using com.nm.interfaces;

namespace com.nm.library.Helper
{
    /// <summary>
    /// 
    /// </summary>
    public static class KmerHelper {
		static string GetKey(HttpSessionState session) {
			return $"SESSIN:{session.SessionID}";
		}

		static int GetCurrentUserId(this HttpSessionState session) {
			return session.Get("CurrentUserId", 0);
		}

		public static void SetCurrentUserId(this Page thePage, int id) {
			thePage.Session.Set("CurrentUserId", id);
		}
		public static void SetCurrentUserId(this MasterPage theMasterPage, int id) {
			theMasterPage.Session.Set("CurrentUserId", id);
		}
		public static void SetCurrentUserId(this ISessionContext theContext, int id) {
			theContext.Session.Set("CurrentUserId", id);
		}

		public static void Logout(this Page thePage) {
			Remove(thePage.Session, "CurrentUserId");
		}

		public static void Logout(this MasterPage theMasterPage) {
			Remove(theMasterPage.Session, "CurrentUserId");
		}

		public static void Logout(this ISessionContext theContext) {
			Remove(theContext.Session, "CurrentUserId");
		}

		public static int GetCurrentUserId(this Page thePage) {
			return thePage.Session.GetCurrentUserId();
		}

		public static int GetCurrentUserId(this MasterPage theMasterPage) {
			return theMasterPage.Session.GetCurrentUserId();
		}

		public static int GetCurrentUserId(this ISessionContext theContext) {
			return theContext.Session.GetCurrentUserId();
		}

		public static HttpSessionState Set(this HttpSessionState session, string pKey, string value) {
			RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
			return session;
		}
		public static HttpSessionState Set(this HttpSessionState session, string pKey, int value) {
			RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
			return session;
		}
		public static HttpSessionState Set(this HttpSessionState session, string pKey, bool value) {
			RedisCache.AddHash(GetKey(session), pKey, value, session.Timeout);
			return session;
		}
		/// <summary>
		/// 从Session中读取字符串
		/// </summary>
		/// <param name="session"></param>
		/// <param name="pKey"></param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static string Get(this HttpSessionState session, string pKey, string dv) {
			var chacheKey = GetKey(session);
			if (!RedisCache.Exists(chacheKey)) return dv;
			RedisCache.Expire(chacheKey, session.Timeout);
			return RedisCache.GetHash(chacheKey, pKey, dv);
		}
		/// <summary>
		/// 从Session中读取整形值
		/// </summary>
		/// <param name="session"></param>
		/// <param name="pKey"></param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static int Get(this HttpSessionState session, string pKey, int dv) {
			var chacheKey = GetKey(session);
			if (!RedisCache.Exists(chacheKey)) return dv;
			RedisCache.Expire(chacheKey, session.Timeout);
			return (int)RedisCache.GetHash(chacheKey, pKey, dv);
		}
		/// <summary>
		/// 从 Session 中读取 布尔值
		/// </summary>
		/// <param name="session"></param>
		/// <param name="pKey"></param>
		/// <param name="dv"></param>
		/// <returns></returns>
		public static bool Get(this HttpSessionState session, string pKey, bool dv) {
			var chacheKey = GetKey(session);
			if (!RedisCache.Exists(chacheKey)) return dv;
			RedisCache.Expire(chacheKey, session.Timeout);
			return (bool)RedisCache.GetHash(chacheKey, pKey, dv);
		}
		public static HttpSessionState Remove(this HttpSessionState session, string pKey) {
			var chacheKey = GetKey(session);
			if (RedisCache.Exists(chacheKey))
				RedisCache.Remove(chacheKey);
			return session;
		}
        /// <summary>
        /// 判断权限
        /// 目前只判断是否审核通过
        /// </summary>
        /// <param name="session"></param>
        /// <returns></returns>
	    public static bool CanIDoIt(this HttpSessionState session){
	        return session.Get("IsVerify", false);
	    }
    }
}